<?php
require_once dirname(__FILE__) . '/common.inc.php';
require_once dirname(__FILE__) . "/DbUtils.class.php";
require_once dirname(__FILE__) . '/libs/phpMailer/class.phpmailer.php';
$myPdo = DbUtils::createPdoInst();

$dbhost = 'pozwebdev.mysql5.webhost.co.nz';
$dbuser = 'ac_user';
$dbpass = 'luckybox4mail';
$dbname = 'advancedcomputers';

$dir = '';
$backupFile = $dbname . date("Y-m-d-H-i-s") . '.sql.gz';
$command = "mysqldump --opt -h$dbhost -u$dbuser -p$dbpass $dbname | gzip > $backupFile";
system($command);
$tmp = DbUtils::get($myPdo, 'pz_profile', NULL, NULL, NULL, NULL, NULL, NULL)->d;
if (count($tmp) > 0) {
  try {
    $mailer = new PHPMailer();
    $mailer->IsSMTP();
    $mailer->Host = $tmp[0]->mail_host;
    
    unset($q->track_id);
    unset($q->order_id);
    $content = 'DB Backup ' . date("Y-m-d H:i:s") . ' ' . getcwd();
    
    $mailer->SMTPAuth = $tmp[0]->mail_SMTPAuth;
    $mailer->Username = $tmp[0]->mail_account;
    $mailer->Password = $tmp[0]->mail_password;
    $mailer->From = 'weida@hhcode.com';
    $mailer->FromName = 'http://hhcode.com';
    $mailer->Body = $content;
    $mailer->Subject = 'DB Backup ' . date("Y-m-d H:i:s");
  
    $mailer->isHTML(true);
    $mailer->AddAddress('acdbbackup@gmail.com', '');
    $mailer->AddAttachment("$backupFile", $backupFile);
    $mailer->Send();
  } catch (phpmailerException $e) {
    echo $e->errorMessage(); //Pretty error messages from PHPMailer
  } catch (Exception $e) {
    echo $e->getMessage(); //Boring error messages from anything else!
  }
  if (file_exists("$dir/_dbbackup/$backupFile")) {
    unlink("$dir/_dbbackup/$backupFile");
  }
}
?>